package web

import (
	"github.com/gin-gonic/gin"
	"marks/marks"
	"net/http"
)

func StartWeb(shell *marks.Shell)  {
	engine := gin.Default()
	engine.LoadHTMLGlob("./web/templates/*")
	engine.StaticFS("/static", http.Dir("./web/static"))
	engine.GET("/", func(context *gin.Context) {
		query := context.Query("detail")
		if query != ""{
			object, e := shell.View(query)
			if e != nil{
				context.JSON(404,nil)
			}else{
				context.JSON(200,object)
			}

		}else{
			info := shell.Chain.GetInfo()

			if len(info )== 0{
				context.HTML(200,"index.html",gin.H{"show":false})

			}else {
				context.HTML(200,"index.html",gin.H{"item":info,"show":true})

			}
		}


	})

	engine.Run(":9000")
}
